#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include <signal.h>
#include<sys/time.h>
#include<linux/posix_types.h>
#include <sys/wait.h>

/*
This is father process
This is father process
This is father process
This is father process
This is father process
Get Signal= 17
This is father process
This is father process

 */

typedef void (*sighandler_t)(int);

sighandler_t oldact;

void handler(int sig){
	if(sig == SIGINT){
		printf("I catch the SIGINT\n");
	}else if(sig == SIGALRM){
		printf("I catch the SIGALRM\n");
	}else if(sig == SIGCHLD){
		//回收子进程
		wait(NULL);
		printf("Get Signal= %d\n", sig);

	}
}

int main(){
	
	pid_t pid;

	//初始化sigaction
	struct sigaction act;
	act.sa_handler = handler;
	act.sa_flags = 0;
	sigemptyset(&act.sa_mask);

	pid = fork();

	if(pid > 0){
		//信号捕捉:SIGCHLD 当子进程状态改变时
		sigaction(SIGCHLD, &act, NULL);

		while(1){
			printf("This is father process\n");
			sleep(1);
		}
	}else if(pid == 0){
		//子进程
		sleep(5);
		exit(0);
	}



	while(1){
		sleep(1);
	}
}
